package com.flyduck.vueshop.system.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.vueshop.common.core.lang.Result;
import com.flyduck.vueshop.common.mybatis.base.BaseController;
import com.flyduck.vueshop.system.admin.service.AdminRoleService;
import com.flyduck.vueshop.system.admin.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * SysMenuController
 * </p>
 *
 * @author flyduck
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/system/admin/role")
public class AdminRoleController extends BaseController {

    @Resource
    private AdminRoleService adminRoleService;

    @GetMapping("/getRolePage")
    @SaCheckPermission("sys:role:list")
    public Result<Page<AdminRoleRespVO>> getRolePage(AdminRoleQueryReqVO adminRoleQueryReqVO){
        Page<AdminRoleRespVO> sysRoleRespVOPage = adminRoleService.getRolePage(getPage(), adminRoleQueryReqVO);
        return Result.success(sysRoleRespVOPage);
    }

    @GetMapping("/getRoleDetailsById/{id}")
    @SaCheckPermission("sys:role:list")
    public Result<AdminRoleDetailsRespVO> getRoleDetailsById(@PathVariable("id") Long id){
        AdminRoleDetailsRespVO adminRoleDetailsRespVO = adminRoleService.getRoleDetailsById(id);
        return Result.success(adminRoleDetailsRespVO);
    }

    @PostMapping("/saveRole")
    @SaCheckPermission("sys:role:save")
    public Result<Void> saveRole(@Validated @RequestBody AdminRoleSaveReqVO adminRoleSaveReqVO){
        adminRoleService.saveRole(adminRoleSaveReqVO);
        return Result.success();
    }

    @PostMapping("/updateRole")
    @SaCheckPermission("sys:role:update")
    public Result<Void> updateRole(@Validated @RequestBody AdminRoleUpdateReqVO adminRoleUpdateReqVO){
        adminRoleService.updateRole(adminRoleUpdateReqVO);
        return Result.success();
    }

    @PostMapping("/batchDeleteByIds")
    @SaCheckPermission("sys:role:delete")
    public Result<Void> batchDeleteByIds(@RequestBody Long[] ids){
        adminRoleService.batchDeleteByIds(Arrays.asList(ids));
        return Result.success();
    }

    @PostMapping("/assignMenusToRole/{roleId}")
    @SaCheckPermission("sys:role:perm")
    public Result<Void> assignMenusToRole(@PathVariable(name = "roleId")Long roleId, @RequestBody Long[] menuIds){
        adminRoleService.assignMenuListToRole(roleId,Arrays.asList(menuIds));
        return Result.success();
    }

}
